﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Warensoft.Unity.Communication.Client.Contracts;

namespace Warensoft.Unity.Communication.Client.Models
{
    public static class DataServicePackageExtensions
    {
        public static DataServicePackage ToDataServicePackage(this byte[] buffer)
        {
            //return DataServicePackage.Create(buffer);
            DataServicePackage package = new DataServicePackage();
            package.FillData(buffer);
            return package;
        }
    }
    /// <summary>
    /// Http 响应协议包类
    /// http response package class
    /// </summary>
    public class HttpResponse
    {
        internal string responseLine;
        /// <summary>
        /// 获取响应行
        /// get the reponse line
        /// </summary>
        public string ResponseLine
        {
            get { return responseLine; }
        }
        private Dictionary<string,string> headers;
        /// <summary>
        /// 获取响应头
        /// get the reponse headers
        /// </summary>
        public Dictionary<string,string> Headers
        {
            get { return headers; }
            set { headers = value; }
        }
        /// <summary>
        /// 获取响应数据正文
        /// get the response bytes
        /// </summary>
        public byte[] Data { get; set; }
        /// <summary>
        /// 请响应数据正文转化为指定类型实例,该类型要继承自IResponseMessage
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetData<T>() where T : IResponseMessage, new()
        {
            T t = new T();
            t.FillData(this.Data);
            return t;
        }
        internal int status;
        /// <summary>
        /// 获取响应状态码
        /// </summary>
        public int Status
        {
            get { return status; }
        }
        /// <summary>
        /// 获取该响应所对应的Http请求
        /// </summary>
        public HttpRequest Request { get; set; }
        /// <summary>
        /// 获取本次响应的文本内容
        /// </summary>
        public string StringContent
        {
            get
            {
                return System.Text.Encoding.UTF8.GetString(this.Data);
            }
        }
    }

}
